package com.expense;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.expense.annotation.Column;
import com.expense.annotation.Table;
import com.expense.dao.Dao;
import com.expense.entity.UserInfo;

public class Test {

	/**
	 * @param args
	 * @throws SQLException 
	 */
	public static void main(String[] args) throws SQLException {
		// TODO Auto-generated method stub
		/*Dao dao=new Dao();
		String sql="select * from personalExpenses_info";
		ResultSet resultSet=dao.selectData(sql);
		
		if(resultSet.next()){
			System.out.println("size:"+1);
		}
		while(resultSet.next()){
			System.out.println(resultSet.getInt("id"));
		}
		dao.CloseCon();*/
		
		
		
		
		
		
		Test test=new Test();
		
		Map<String, Object> valueMap=new HashMap<String, Object>();
		valueMap.put("id", 1);
		valueMap.put("userName", "admin");
		valueMap.put("userPassWord", "admin");
		valueMap.put("empName", "aaa");
		valueMap.put("email", "aaa");
		valueMap.put("isSendEmail", 1);
		valueMap.put("state", 1);
		
		Object object=test.setClassObjInfo(new UserInfo(), valueMap);
		test.printClassObjInfo(object);
	}
	
	
	
	
	/**
	 * 为对象设置值
	 * 
	 * **/ 
	public Object setClassObjInfo(Object object,Map<String, Object> valueMap){
		
		Field[] fields=object.getClass().getDeclaredFields(); 
	    for(int i =0;i<fields.length;i++){
	       Field field = fields[i];
	       String fieldName= field.getName(); //属性名称
	       Class fieldClass = field.getType(); //获取属性字段类型
	       //System.out.println("属性名称:"+  fieldName+" 的类型:"+fieldClass);
	       String columnName="";
	       //判断该成员变量上是不是存在Column类型的注解   
           if(field.isAnnotationPresent(Column.class)){   
              //存在   
              Column column = field.getAnnotation(Column.class);//获取实例   
               //获取元素值   
               columnName = column.columnName(); 
           }
           //这里是里设置属性的值 
	      Method method;
		try {
			
			method = object.getClass().getMethod("set"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1),fieldClass);
 
			Object value=null;
			if(valueMap.containsKey(columnName)){
				value=valueMap.get(columnName);
			}
			method.invoke(object,value);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	       

	    }
		return object;
	}
	
	
	/**
	 * 输出对象的各字段值
	 * **/
	public void printClassObjInfo(Object object){
		//System.out.println("类名:"+object.getClass().getName());
		String tableName="";
		//判断该类上是不是存在Table类型的注解   
        if(object.getClass().isAnnotationPresent(Table.class)){   
           //存在   
        	Table table = (Table) object.getClass().getAnnotation(Table.class);//获取实例   
            //获取元素值   
        	tableName = table.tableName(); 
        	
        	//System.out.println("表名是:"+tableName);
        }else{
        	//System.out.println("没有指定表名");
        }  
		
       
        
		
		Field[] fields=object.getClass().getDeclaredFields(); 
	    for(int i =0;i<fields.length;i++){
	       Field field = fields[i];
	       String fieldName= field.getName(); //属性名称
	       Class fieldClass = field.getType(); //获取属性字段类型
	       String fieldTypeName=fieldClass.getName();//类型名称
	       String fieldSimpleTypeName=fieldClass.getSimpleName();//简短类型名称
	      // System.out.println("属性名称:"+  fieldName);
	       System.out.println("属性名称:"+  fieldName+" 的类型:"+fieldSimpleTypeName);
	       
	       String columnName="";
	       //判断该成员变量上是不是存在Column类型的注解   
           if(field.isAnnotationPresent(Column.class)){   
              //存在   
              Column column = field.getAnnotation(Column.class);//获取实例   
               //获取元素值   
               columnName = column.columnName(); 
               
              // System.out.println("属性字段:"+  fieldName+"列名是"+columnName);
           }else{
        	   
        	  // System.out.println("属性字段:"+  fieldName+"没有指定列名");
           }  
           
           
		//   这里是里获取属性的值 
           Method method;
		try {
			Object value="";
			method =object.getClass().getMethod("get"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1), null) ;
			//objClass.getMethod("get"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1),null);
			value=method.invoke(object,null);
			//System.out.println("属性字段:"+  fieldName+"的值是"+value);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	      
	   }
		
	}
	
	
	
	
	

}
